<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="评论时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="评论人" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入评论人名字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="几星评论" prop="star">
        <el-select
          v-model="queryParams.star"
          placeholder="请输入几星评论"
          clearable
          @keyup.enter.native="handleQuery"
        >
        <el-option v-for="star in stars" :key="star" :label="`${star}星`" :value="star"></el-option>
      </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:comment:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:comment:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:comment:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:comment:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="commentList" @selection-change="handleSelectionChange">
      <el-table-column  type="selection" width="55" align="center"  />
      <el-table-column label="评论ID" align="center" prop="commentId" width="80"/>
      <el-table-column label="评论房源名称" align="center" prop="sysProperty.name" />
      <el-table-column label="评论人" align="center" prop="createBy" width="100"/>
      <el-table-column label="评论内容" align="center" prop="content" />
      <el-table-column label="评论时间" align="center" prop="commentTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.commentTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="评论图片" align="center" prop="commentPhoto" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.commentPhoto" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="逻辑删除" align="center" prop="delFlag" />
      <el-table-column label="几星评论" align="center" prop="star">
        <template slot-scope="scope">
          <el-rate :value="scope.row.star" disabled show-text></el-rate>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:comment:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:comment:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total-3"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改评论详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="评论内容">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-form-item label="评论图片" prop="commentPhoto">
          <image-upload v-model="form.commentPhoto"/>
        </el-form-item>
        <el-form-item label="几星评论" prop="star">
          <el-rate v-model="form.star" allow-half show-text placeholder="请输入几星评论" ></el-rate>
        </el-form-item>
        <el-divider content-position="center">房源评论id关联表信息</el-divider>
        <el-table v-loading="loading" :data="propertyList" @selection-change="handleSelectionChangeProperty">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="房源ID" align="center" prop="propertyId" />
          <el-table-column label="房源名称" align="center" prop="name" />
          <el-table-column label="地址" align="center" prop="address" />
          <el-table-column label="房东" align="center" prop="homeowner.userName" />
          <el-table-column label="价格" align="center" prop="price" />
          <el-table-column label="图片" align="center" prop="photo" width="100">
            <template slot-scope="scope">
              <image-preview :src="scope.row.photo" :width="50" :height="50"/>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" align="center" prop="creatTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.creatTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="其他相关信息" align="center" prop="otherInfo" />  
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
      <pagination
      v-show="total>0"
      :total="total-1"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getListProperty"
    />
    </el-dialog>
  </div>
</template>

<script>
import { listComment, getComment, delComment, addComment, updateComment } from "@/api/system/comment";
import{listProperty} from "@/api/system/property"

export default {
  name: "Comment",
  dicts: ['sys_property_status', 'sys_deposit_rule', 'sys_property_type', 'sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      stars: ['0', '1', '2', '3', '4', '5'], // 星级选项
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedSysProperty: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 评论详情表格数据
      commentList: [],
      existingPropertyIds: {},
      // 房源信息表格数据
      propertyList: [],
      //评论房源信息关联表修改所需数据
      sysPropertyCommentList: [],
      //日期范围
      dateRange: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        commentTime: null,
        star: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        star: [{
          required: true,
          message: '评分不能为空',
          trigger: 'change'
        }],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询评论详情列表 */
    getList() {
      this.loading = true;
      listComment(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.commentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      
    },
    /** 查询房源信息列表 */
    getListProperty() {
    this.loading = true;
    const queryParams = this.addDateRange(this.queryParams, this.dateRange);
    return listProperty(queryParams).then(response => {
        this.propertyList =  response.rows;
        this.total = response.total;
        this.loading = false;
    });
},
  filterPropertyList(sysPropertyCommentList) {
    if (!this.propertyList || !sysPropertyCommentList) {
        // 如果 propertyList 或 sysPropertyCommentList 不存在，无法进行过滤
        console.error('Property list or sysPropertyCommentList is not available.');
        return;
    }

    // 创建一个 Set 来存储 sysPropertyCommentList 中存在的 propertyId
    const existingPropertyIds = new Set();
    for (const comment of sysPropertyCommentList) {
        existingPropertyIds.add(comment.propertyId);
    }

    // 使用 filter 方法过滤 propertyList
    this.propertyList = this.propertyList.filter(property => existingPropertyIds.has(property.propertyId));
},

    
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.getList();
    },
    // 表单重置
    reset() {
      this.form = {
        commentId: null,
        content: null,
        commentTime: null,
        commentPhoto: null,
        delFlag: null,
        star: null,
        ids: [],
      };
      this.propertyList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 评论多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.commentId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
     // 房源多选框选中数据
    handleSelectionChangeProperty(selection) {
      this.ids = selection.map(item => item.propertyId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加评论详情";
      this.getListProperty();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.isUpdate = true; // 设置标志位为 true，表示是修改查询
      const commentId = row.commentId || this.ids
      getComment(commentId).then(response => {
        this.form = response.data;
        // 更新 sysPropertyCommentList 数据
        const sysPropertyCommentList = response.data.sysPropertyCommentList;
        // 获取 propertyList 数据
        this.getListProperty().then(() => {
            // 然后执行过滤操作
            this.filterPropertyList(sysPropertyCommentList);
            // 更新总数
            this.total = this.propertyList.length;
        });
        this.open = true;
        this.title = "修改评论详情";
      });
      this.getList() 
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.ids = this.ids
          // this.form.propertyList = this.propertyList;
          if (this.form.commentId != null) { 
            updateComment(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addComment(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const commentIds = row.commentId || this.ids;
      this.$modal.confirm('是否确认删除评论详情编号为"' + commentIds + '"的数据项？').then(function() {
        return delComment(commentIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 房源评论id关联表序号 */
    // rowSysPropertyIndex({ row, rowIndex }) {
    //   row.index = rowIndex + 1;
    // },
    /** 房源评论id关联表删除按钮操作 */
    handleDeleteSysProperty() {
      if (this.checkedSysProperty.length == 0) {
        this.$modal.msgError("请先选择要删除的房源评论id关联表数据");
      } else {
        const propertyList = this.propertyList;
        const checkedSysProperty = this.checkedSysProperty;
        this.propertyList = propertyList.filter(function(item) {
          return checkedSysProperty.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleSysPropertySelectionChange(selection) {
      this.checkedSysProperty = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/comment/export', {
        ...this.queryParams
      }, `comment_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style>

</style>

